//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "Collar.h"
using namespace Cephei::QL::Instruments;
#include <gen/QL/CashFlow.h>
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/Cashflows/FloatingRateCoupon.h>
#include <gen/QL/Instruments/CapFloor.h>
#include <gen/QL/PricingEngine.h>
using namespace Cephei::QL;
using namespace Cephei::QL::Termstructures;
using namespace Cephei::QL::Cashflows;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Instruments::CCollar::CCollar (Cephei::Core::IVector<Cephei::QL::ICashFlow^>^ floatingLeg, Cephei::Core::IVector<Double>^ capRates, Cephei::Core::IVector<Double>^ floorRates, Cephei::QL::IPricingEngine^ QL_Pricer) : CCapFloor(CCollar::typeid)
{
    try
    {
#ifdef HANDLE
        _phCollar = NULL;
#endif
        floatingLeg ->Lock ();
        INativeVector<Cephei::QL::ICashFlow^>^ _NCIfloatingLeg = floatingLeg->getFeature (NativeFeature::shared_ptr);
        CCashFlowVector^ _NCfloatingLeg = safe_cast<CCashFlowVector^>(_NCIfloatingLeg);
        std::vector<boost::shared_ptr<QuantLib::CashFlow> >& _floatingLeg = static_cast<std::vector<boost::shared_ptr<QuantLib::CashFlow> >&> (_NCfloatingLeg->GetShared ());
        capRates->Lock();
        INativeVector<Double>^ _NCIcapRates = capRates->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCcapRates = safe_cast<CDoubleVector^>(_NCIcapRates);
        std::vector<QuantLib::Rate>& _capRates = static_cast<std::vector<QuantLib::Rate>&> (_NCcapRates->GetReference ());
        floorRates->Lock();
        INativeVector<Double>^ _NCIfloorRates = floorRates->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCfloorRates = safe_cast<CDoubleVector^>(_NCIfloorRates);
        std::vector<QuantLib::Rate>& _floorRates = static_cast<std::vector<QuantLib::Rate>&> (_NCfloorRates->GetReference ());
        _ppCollar = new boost::shared_ptr<QuantLib::Collar> (new QuantLib::Collar ( _floatingLeg,  _capRates,  _floorRates ));
        CPricingEngine^ _CQL_Pricer = safe_cast<CPricingEngine^> (QL_Pricer);
        boost::shared_ptr<QuantLib::PricingEngine>& _QL_Pricer = static_cast<boost::shared_ptr<QuantLib::PricingEngine>&> (_CQL_Pricer->GetShared ());
        (*_ppCollar)->setPricingEngine (_QL_Pricer);
        SetCapFloor (boost::dynamic_pointer_cast<QuantLib::CapFloor> (*_ppCollar));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (floatingLeg != nullptr) floatingLeg->Unlock();    //not optional
        if (capRates != nullptr) capRates->Unlock();    //not optional
        if (floorRates != nullptr) floorRates->Unlock();    //not optional
    }
}
Cephei::QL::Instruments::CCollar::CCollar (boost::shared_ptr<QuantLib::Collar>& childNative, Object^ owner) : CCapFloor(CCollar::typeid)
{
#ifdef HANDLE
	_phCollar = NULL;
#endif
	_ppCollar = &childNative;
    _ppCapFloor = new boost::shared_ptr<QuantLib::CapFloor> (boost::dynamic_pointer_cast<QuantLib::CapFloor> (*_ppCollar));
}
Cephei::QL::Instruments::CCollar::CCollar (QuantLib::Collar& childNative, Object^ owner) : CCapFloor(CCollar::typeid)
{
#ifdef HANDLE
	_phCollar = NULL;
#endif
	_ppCollar = new boost::shared_ptr<QuantLib::Collar> (&childNative);
    _ppCapFloor = new boost::shared_ptr<QuantLib::CapFloor> (boost::dynamic_pointer_cast<QuantLib::CapFloor> (*_ppCollar));
    _CollarOwner = owner;
    _CapFloorOwner = owner;
}

Cephei::QL::Instruments::CCollar::CCollar (CCollar^ copy) : CCapFloor(CCollar::typeid)
{
#ifdef HANDLE
	_phCollar = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppCollar = new boost::shared_ptr<QuantLib::Collar> (copy->GetShared());
        _ppCapFloor = new boost::shared_ptr<QuantLib::CapFloor> (boost::dynamic_pointer_cast<QuantLib::CapFloor> (*_ppCollar));
    }
}
Cephei::QL::Instruments::CCollar::CCollar (PLATFORM::Type^ t) : CCapFloor(CCollar::typeid)
{
#ifdef HANDLE
	_phCollar = NULL;
#endif
	if (!t->IsSubclassOf(CCollar::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Instruments::CCollar::CCollar (QuantLib::Handle<QuantLib::Collar>& childNative, Object^ owner)  : CCapFloor(CCollar::typeid)
{
	_phCollar = &childNative;
	_ppCollar = &static_cast<boost::shared_ptr<QuantLib::Collar>>(childNative.currentLink());
    _ppCapFloor = new boost::shared_ptr<QuantLib::CapFloor> (boost::dynamic_pointer_cast<QuantLib::CapFloor> (*_ppCollar));
    _CollarOwner = owner;
}
Cephei::QL::Instruments::CCollar::CCollar (QuantLib::Handle<QuantLib::Collar> childNative)  : CCapFloor(CCollar::typeid)
{
	_phCollar = &childNative;
	_ppCollar = &static_cast<boost::shared_ptr<QuantLib::Collar>>(childNative.currentLink());
    _ppCapFloor = new boost::shared_ptr<QuantLib::CapFloor> (boost::dynamic_pointer_cast<QuantLib::CapFloor> (*_ppCollar));
}
#endif
#ifdef STRUCT
Cephei::QL::Instruments::CCollar::CCollar (QuantLib::Collar childNative)  : CCapFloor(CCollar::typeid)
{
#ifdef HANDLE
	_phCollar = NULL;
#endif
	_ppCollar = new boost::shared_ptr<QuantLib::Collar> (new QuantLib::Collar (childNative));
    _ppCapFloor = new boost::shared_ptr<QuantLib::CapFloor> (boost::dynamic_pointer_cast<QuantLib::CapFloor> (*_ppCollar));
}
#endif

Cephei::QL::Instruments::CCollar::~CCollar ()
{
    if (_ppCollar != NULL)
    {
	    delete _ppCollar;
        _ppCollar = NULL;
    }
}
Cephei::QL::Instruments::CCollar::!CCollar ()
{
    if (_ppCollar != NULL)
    {
	    delete _ppCollar;
    }
}
QuantLib::Collar& Cephei::QL::Instruments::CCollar::GetReference ()
{
    if (_ppCollar == NULL) throw REFNEW NativeNullException ();
	return **_ppCollar;
}
boost::shared_ptr<QuantLib::Collar>& Cephei::QL::Instruments::CCollar::GetShared ()
{
    if (_ppCollar == NULL) throw REFNEW NativeNullException ();
	return *_ppCollar;
}
QuantLib::Collar* Cephei::QL::Instruments::CCollar::GetPointer ()
{
    if (_ppCollar == NULL) throw REFNEW NativeNullException ();
	return &**_ppCollar;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::Collar>& Cephei::QL::Instruments::CCollar::GetHandle ()
{
	if (_phCollar == NULL)
	{
		_phCollar = new Handle<QuantLib::Collar> (*_ppCollar);
	}
	return *_phCollar;
}
#endif
bool Cephei::QL::Instruments::CCollar::HasNative () 
{
	return (_ppCollar != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Instruments::ICollar^ Cephei::QL::Instruments::CCollar_Factory::Create (Cephei::Core::IVector<Cephei::QL::ICashFlow^>^ floatingLeg, Cephei::Core::IVector<Double>^ capRates, Cephei::Core::IVector<Double>^ floorRates, Cephei::QL::IPricingEngine^ QL_Pricer)
{
    return REFNEW CCollar ( floatingLeg,  capRates,  floorRates,  QL_Pricer);
}
